﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;

using Microsoft.Practices.Unity.Configuration;
using Microsoft.Practices.Unity;

namespace Arquitetura.Architectural.IoC
{
    public static class UnityContextHelper
    {
        #region Constante(s)
        public const string DefaultSectionName = "unity";
        #endregion Constante(s)

        #region Método(s)
        public static IUnityContainer LoadDefaultConfig(this IUnityContainer container)
        {
            container.LoadDefaultConfig(DefaultSectionName);
            return container;
        }

        public static IUnityContainer LoadDefaultConfig(this IUnityContainer container, string sectionName)
        {
            GetUnityConfigurationSection(sectionName).Containers.Default.Configure(container);
            return container;
        }

        public static IUnityContainer LoadCustomConfig(this IUnityContainer container, string containerName)
        {
            container.LoadCustomConfig(DefaultSectionName, containerName);
            return container;
        }

        public static IUnityContainer LoadCustomConfig(this IUnityContainer container, string sectionName, string containerName)
        {
            GetUnityConfigurationSection(sectionName).Containers[containerName].Configure(container);
            return container;
        }

        private static UnityConfigurationSection GetUnityConfigurationSection(string sectionName)
        {
            return (UnityConfigurationSection)ConfigurationManager.GetSection(sectionName);
        }
        #endregion Método(s)
    }
}
